﻿using System;
using Microsoft.SharePoint;
using VPLink2013.Framework;
using VPLink2013.Framework.Constants;

namespace VPLink2013.Webpart
{
    public class MenuInterneAnnonce : WebpartVPLink
    {
        #region get HTML
        /// <summary>
        /// Returns the HTML code rendered by this web part
        /// </summary>
        /// <returns>HTL code as string</returns>
        protected override string GetHtml()
        {
            ContextTrace.PrefixFileName = "webpart MenuInterneAnnonce";
            string strHtml = ContextTrace.PrefixFileName;

            try
            {
                string strUrl = ParamSharepointSite;
                ContextTrace.AddTrace("strUrl : " + strUrl);
                using (SPSite site = new SPSite(strUrl))
                {
                    using (SPWeb web = site.RootWeb)
                    {
                        SPList lst = web.Lists.TryGetList(ListTitles.NewsVPTitle);
                        if (lst != null)
                        {
                            ProcessMenuAnnonces(lst);
                        }
                    }
                }
                strHtml = sbHtml.ToString();
            }
            catch (System.Exception exc)
            {
                ContextTrace.AddTrace("Erreur GetHtml : " + exc.Message + "/" + exc.StackTrace);
            }
            return strHtml;
        }
        public string GetHtmlMenuAnnonces()
        {
            ContextTrace.PrefixFileName = "webpart MenuInterneAnnonce";
            string strHtml = ContextTrace.PrefixFileName;

            try
            {
                string strUrl = ParamSharepointSite;
                ContextTrace.AddTrace("strUrl : " + strUrl);
                using (SPSite site = new SPSite(strUrl))
                {
                    using (SPWeb web = site.RootWeb)
                    {
                        SPList lst = web.Lists.TryGetList(ListTitles.NewsVPTitle);
                        if (lst != null)
                        {
                            ProcessMenuAnnonces(lst);
                        }
                    }
                }
                strHtml = sbHtml.ToString();
            }
            catch (System.Exception exc)
            {
                ContextTrace.AddTrace("Erreur GetHtml : " + exc.Message + "/" + exc.StackTrace);
            }
            return strHtml;
        }
        #endregion get HTML

        private void ProcessMenuAnnonces(SPList lst)
        {
            string strPage = this.Context.Request.Url.AbsolutePath.ToLower();
            string strCategorieCrt = ParamFrm("Categorie");
            if (strPage.Contains("detail"))
            {
                string strItemID = ParamFrm("ItemID");
                int itemID = int.Parse(strItemID);
                SPListItem item = lst.GetItemById(itemID);
                strCategorieCrt = VPLink2013SPHelper.GetValue(item, "Categorie");
            }
            ContextTrace.AddTrace("strCategorieCrt : " + strCategorieCrt);

            sbHtml.AppendLine("<div class=\"bloc_menuArt\">");
            sbHtml.AppendLine("<ul>");

            string strUrlListeAnnonces = ParamSharepointSite + "/Pages/Annonces2013.aspx";

            string strClassActif = string.Empty;
            if (string.IsNullOrEmpty(strCategorieCrt))
            {
                strClassActif = " class=\"actif\"";
            }
            SetLink("Toutes les news", strUrlListeAnnonces, strClassActif);
            strClassActif = " class=\"actif\"";

            SPFieldChoice fldCategorie = (SPFieldChoice)lst.Fields["Categorie"];
            foreach (string strChoice in fldCategorie.Choices)
            {
                string strUrl = strUrlListeAnnonces + "?Categorie=" + strChoice;
                if (strChoice == strCategorieCrt)
                {
                    SetLink(strChoice, strUrl, strClassActif);
                }
                else
                {
                    SetLink(strChoice, strUrl, string.Empty);
                }
            }

            SPList lstConfig = lst.ParentWeb.Lists.TryGetList(ListTitles.ConfigurationVPTitle);
            int iCountMonth = 0;
            if (lstConfig != null)
            {
                string strCountMonth = GetValueConfigurationVP(lstConfig, "NombreMoisMenu");
                if (!string.IsNullOrEmpty(strCountMonth))
                {
                    iCountMonth = int.Parse(strCountMonth);
                }
            }
            if (iCountMonth > 0)
            {
                sbHtml.AppendLine("<li><span class='newsarchives'>Archives par mois</span></li>");
                string strMoisCrt = ParamFrm("MoisCrt");
                for (int i = 0; i < iCountMonth; i++)
                {
                    DateTime dtDebutMois = DateTime.Now.AddMonths(-1 * i);
                    dtDebutMois = new DateTime(dtDebutMois.Year, dtDebutMois.Month, 1);

                    string strMois = dtDebutMois.ToString("dd/MM/yyyy");
                    string strTitreMois = dtDebutMois.ToString("MMMM yyyy");
                    strTitreMois = strTitreMois.Substring(0, 1).ToUpper() + strTitreMois.Substring(1);

                    string strUrl = strUrlListeAnnonces + "?MoisCrt=" + strMois;
                    if (strMois == strMoisCrt)
                    {
                        SetLink(strTitreMois, strUrl, strClassActif);
                    }
                    else
                    {
                        SetLink(strTitreMois, strUrl, "");
                    }
                }
            }

            sbHtml.AppendLine("</ul>");
            sbHtml.AppendLine("</div>");
        }

        private void SetLink(string titre, string url, string cssClass)
        {
            string titrePiwik = titre.Replace("/", "-").Replace("'", "\\'").Replace("\"", "\\'");
            string strLink = "<li" + cssClass + "><a href=\"" + url + "\" onclick=\"javascript:_paq.push(['trackPageView', 'NEWS/" + titrePiwik + "']);\"><nobr>" + titre + "</nobr></a></li>";
            sbHtml.AppendLine(strLink);
            ContextTrace.AddTrace("SetLink : " + titre + " => " + url);
        }
    }
}
